/**
* author: deepanshu
* created: 20-02-2023 02:19:09
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (ll i=a; i<(b); i++)
#define F0R(i, a) for (ll i=0; i<(a); i++)
#define mod 1000000007
#define getl(a) cin>>a;
#define init(a,n) for(ll i=0; i<n; i++) { cin>>a[i]; }
#define all(v) v.begin(),v.end()
void make_set(ll n, vector<ll> &parent, vector<ll> &ranks)
{
for (ll i = 0; i < n; i++)
{
parent[i] = i;
ranks[i] = 0;
}
}
ll find_par(ll v, vector<ll> &parent)
{
if (v == parent[v])
{
return v;
}
return parent[v] = find_par(parent[v], parent);
}
void union_set(ll u, ll v, vector<ll> &parent, vector<ll> &ranks)
{
ll pu = find_par(u, parent);
ll pv = find_par(v, parent);
if (pu != pv)
{
if (ranks[pu] > ranks[pv])
{
parent[pv] = pu;
}
else if (ranks[pu] < ranks[pv])
{
parent[pu] = pv;
}
else
{
parent[pu] = pv;
ranks[pv]++;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll p,k,i,j;
cin>>p>>k;
vector<ll>parent(p),ranks(p);
make_set(p,parent,ranks);
map<ll,ll>mp,mp1;
ll x=0;
for(i=0;i<p;i++)
{
if(i==((i*k)%p))
{
x++;
}
}
for(i=1;i<p;i++)
{
ll rem = (i*k)%p;
if(rem!=0)
{
union_set(i,rem,parent,ranks);
}
}
for(i=1;i<p;i++)
{
mp[find_par(i,parent)]++;
}
// cout<<mp.size();
ll ans=1;
for(i=0;i<mp.size();i++)
{
ans*=p;
ans%=mod;
}
ans*=x;
ans%=mod;
cout<<ans;
return 0;
}
1217. Minimum Cost to Move Chips to The Same Position | 347. Top K Frequent Elements |
1503. Last Moment Before All Ants Fall Out of a Plank | 430. Flatten a Multilevel Doubly Linked List |
1290. Convert Binary Number in a Linked List to Integer | 1525. Number of Good Ways to Split a String |
72. Edit Distance | 563. Binary Tree Tilt |
1306. Jump Game III | 236. Lowest Common Ancestor of a Binary Tree |
790. Domino and Tromino Tiling | 878. Nth Magical Number |
2099. Find Subsequence of Length K With the Largest Sum | 1608A - Find Array |
416. Partition Equal Subset Sum | 1446. Consecutive Characters |
1618A - Polycarp and Sums of Subsequences | 1618B - Missing Bigram |
938. Range Sum of BST | 147. Insertion Sort List |
310. Minimum Height Trees | 2110. Number of Smooth Descent Periods of a Stock |
2109. Adding Spaces to a String | 2108. Find First Palindromic String in the Array |
394. Decode String | 902. Numbers At Most N Given Digit Set |
221. Maximal Square | 1200. Minimum Absolute Difference |
1619B - Squares and Cubes | 1619A - Square String |